<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2018/11/5
 * Time: 11:28
 */

namespace Professor\Controller;

use Common\Controller\MemberbaseController;
use Professor\Model\ProfessorModel;

class ProfessorController extends MemberbaseController {

    protected $pf_id;

    function _initialize() {
        parent::_initialize();
        $user_id    = sp_get_current_userid();
        $re         = M('professor')->where(['user_id' => $user_id])->find();
        if ($re) {
            $this->pf_id = $re['id'];
        } else {
            $this->pf_id = 0;
        }
    }

    // 专栏作者编辑个人信息
    public function professor_edit()
    {
        $user           = sp_get_current_user();
        $user_id        = $user['id'];
        $professor_info = M('professor')->where(['user_id' => $user_id])->find();
        if (IS_POST) {

            $data = array(
                'user_id'           => $user_id,
                'introduction'      => I('post.introduction'),
                'updated_at'        => time(),
            );

            if (empty($user['user_nicename']) || empty($user['avatar']) || empty($user['birthday']) || empty($user['city_id']) || empty($user['position']) || empty($user['company']) || empty($user['signature'])) {
                $this->error("请先完善基础信息！");
            }

            if (!pre_data($data)) {
                $this->error("请完善必要信息！");
            }

            if ($professor_info) { // 更改信息
                $re = M('professor')->where(['id' => $professor_info['id']])->save($data);
                if ($re) {
                    $this->success("修改成功");
                }
            } else { // 新申请成为专栏作者
                $data['created_at'] = time();
                $data['status']     = ProfessorModel::PF_STATUS_WAIT_VERIFY;
                $pf_id              = M('professor')->add($data);
                if ($pf_id) {
                    $this->_link_pf_info($pf_id);
                    $this->success("申请成功，请等待审核", leuu("user/center/index"));
                } else {
                    $this->error("申请失败，请稍后再试");
                }
            }
        } else {

            $this->assign('area', json_encode(getArea(2)));
            if ($user['mobile']) {
                $user['short_mobile'] = substr_replace($user['mobile'],'****',3,4);
            }
            if ($user['company_scale'] == 0) {
                $user['scale'] = "不明确";
            } elseif ($user['company_scale'] <= 20 && $user['company_scale'] > 0) {
                $user['scale'] = "1~20人";
            } elseif ($user['company_scale'] <= 100 && $user['company_scale'] > 20) {
                $user['scale'] = "20~100人";
            } elseif ($user['company_scale'] <= 500 && $user['company_scale'] > 100) {
                $user['scale'] = "100~500人";
            } elseif ($user['company_scale'] <= 1000 && $user['company_scale'] > 500) {
                $user['scale'] = "500~1000人";
            } elseif ($user['company_scale'] <= 5000 && $user['company_scale'] > 1000) {
                $user['scale'] = "1000~5000人";
            } else {
                $user['scale'] = "5000人以上";
            }
            $this->assign($user);
            if ($this->user['city_id'] > 0) {
                $path = getAreaParentPath($this->user['city_id'], getArea(2));
                $path = $path ? $path : '';
                $this->assign('path', json_encode($path));
            }

            if ($professor_info) {
                $this->assign('professor_info', $professor_info);
            }

            $where = ['user_id' => $user_id, 'del_time' => 0];
            // 项目经历
            $pf_project = M('pf_project')->where($where)->select();
            !empty($pf_project) && $this->assign('pf_project', $pf_project);

            // 教育经历
            $pf_education = M('pf_education')->where($where)->select();
            !empty($pf_education) && $this->assign('pf_education', $pf_education);

            // 出版作品
            $pf_publish = M('pf_publish')->where($where)->select();
            !empty($pf_publish) && $this->assign('pf_publish', $pf_publish);

            // 资格认证
            $pf_certificate = M('pf_certificate')->where($where)->select();
            !empty($pf_certificate) && $this->assign('pf_certificate', $pf_certificate);

            // 专利发明
            $pf_patent = M('pf_patent')->where($where)->select();
            !empty($pf_patent) && $this->assign('pf_patent', $pf_patent);

            // 荣誉奖项
            $pf_award = M('pf_award')->where($where)->select();
            !empty($pf_award) && $this->assign('pf_award', $pf_award);

            $this->display();
        }
    }


    // 编辑专栏作者项目
    public function pf_project_edit()
    {
        if (IS_POST) {
            $id = I('post.id', 0, 'intval');

            // 删除
            if ($_POST['delete']) {
                if (M('pf_project')->where(['id' => $id, 'del_time' => 0])->find()) {
                    $data['del_time'] = time();
                    M('pf_project')->where(['id' => $id])->save($data);
                    $this->success("删除成功");
                } else {
                    $this->error("请求数据有误！");
                }
            }

            $data = array(
                'user_id'           => sp_get_current_userid(),
                'pf_id'             => $this->pf_id,
                'project_name'      => I('post.project_name'),
                'start_time'        => I('post.start_time'),
                'end_time'          => I('post.end_time'),
                'project_desc'      => I('post.project_desc'),
            );

            if (!pre_data($data, ['pf_id', 'end_time'])) {
                $this->error("请完善必要信息");
            }

            if ($id) {
                // 更新
                M('pf_project')->where(['id' => $id])->save($data);
                if (sp_is_mobile()) {
                    $this->success('编辑成功', U('Professor/Professor/professor_edit'));exit;
                }
                $data = array('status' => 1, 'id' => $id);
                if (sp_is_mobile()) {
                    $this->success('编辑成功', U('Professor/Professor/professor_edit'));exit;
                }
                $this->ajaxReturn($data);

            } else {
                // 新建
                $id = M('pf_project')->add($data);
                $data = array('status' => 1, 'id' => $id);
                $this->ajaxReturn($data);
            }

        } else {
            if ($id = I("get.id", 0, 'intval')) {
                $pf_project = M('pf_project')->where(['id' => $id, 'del_time' => 0])->find();
                $this->assign('pf_project', $pf_project);
            }
            $this->display();
        }
    }

    // 编辑专栏作者教育经历
    public function pf_education_edit()
    {
        if (IS_POST) {
            $id = $_POST['id'];

            // 删除
            if ($_POST['delete']) {
                if ($info = M('pf_education')->where(['id' => $id, 'del_time' => 0])->find()) {
                    $data['del_time'] = time();
                    M('pf_education')->where(['id' => $id])->save($data);
                    $this->success("删除成功");
                } else {
                    $this->error("请求数据有误！");
                }
            }

            $data = array(
                'user_id'       => sp_get_current_userid(),
                'pf_id'         => $this->pf_id,
                'school'        => I('post.school'),
                'start_time'    => I('post.start_time'),
                'end_time'      => I('post.end_time'),
                'major'         => I('post.major'),
                'degree'        => I('post.degree'),
                'experience'    => I('post.experience'),
            );

            if (!pre_data($data, ['pf_id', 'end_time', 'experience'])) {
                $this->error("请完善必要信息");
            }

            if ($id) {
                // 更新
                M('pf_education')->where(['id' => $id, 'del_time' => 0])->save($data);
                if (sp_is_mobile()) {
                    $this->success('编辑成功', U('Professor/Professor/professor_edit'));exit;
                }
                $data = array('status' => 1, 'id' => $id);
                $this->ajaxReturn($data);
            } else {
                // 新建
                $id = M('pf_education')->add($data);
                if (sp_is_mobile()) {
                    $this->success('编辑成功', U('Professor/Professor/professor_edit'));exit;
                }
                $data = array('status' => 1, 'id' => $id);
                $this->ajaxReturn($data);
            }

        } else {
            if ($id = I("get.id", 0, 'intval')) {
                $pf_education = M('pf_education')->where(['id' => $id, 'del_time' => 0])->find();
                $this->assign('pf_education', $pf_education);
            }
            $this->display();
        }
    }


    // 编辑专栏作者出版作品
    public function pf_publish_edit()
    {
        if (IS_POST) {
            $id = $_POST['id'];

            // 删除
            if ($_POST['delete']) {
                if ($info = M('pf_publish')->where(['id' => $id, 'del_time' => 0])->find()) {
                    $data['del_time'] = time();
                    M('pf_publish')->where(['id' => $id])->save($data);
                    $this->success("删除成功");
                } else {
                    $this->error("请求数据有误！");
                }
            }

            $data = array(
                'user_id'           => sp_get_current_userid(),
                'pf_id'             => $this->pf_id,
                'article_name'      => I('post.article_name'),
                'magazine'          => I('post.magazine'),
                'publish_time'      => I('post.publish_time'),
                'publish_house'     => I('post.publish_house'),
            );

            if (!pre_data($data)) {
                $this->error("请完善必要信息");
            }

            if ($id) {
                // 更新
                M('pf_publish')->where(['id' => $id, 'del_time' => 0])->save($data);
                if (sp_is_mobile()) {
                    $this->success('编辑成功', U('Professor/Professor/professor_edit'));exit;
                }
                $data = array('status' => 1, 'id' => $id);
                $this->ajaxReturn($data);
            } else {
                // 新建
                $id = M('pf_publish')->add($data);
                if (sp_is_mobile()) {
                    $this->success('编辑成功', U('Professor/Professor/professor_edit'));exit;
                }
                $data = array('status' => 1, 'id' => $id);
                $this->ajaxReturn($data);
            }

        } else {
            if ($id = I("get.id", 0, 'intval')) {
                $pf_publish = M('pf_publish')->where(['id' => $id, 'del_time' => 0])->find();
                $this->assign('pf_publish', $pf_publish);
            }
            $this->display();
        }
    }


    // 编辑专栏作者资格认证
    public function pf_certificate_edit()
    {
        if (IS_POST) {
            $id = $_POST['id'];

            // 删除
            if ($_POST['delete']) {
                if ($info = M('pf_certificate')->where(['id' => $id, 'del_time' => 0])->find()) {
                    $data['del_time'] = time();
                    M('pf_certificate')->where(['id' => $id])->save($data);
                    $this->success("删除成功");
                } else {
                    $this->error("请求数据有误！");
                }
            }

            $data = array(
                'user_id'               => sp_get_current_userid(),
                'pf_id'                  => $this->pf_id,
                'certificate_name'      => I('post.certificate_name'),
                'certificate_no'        => I('post.certificate_no'),
                'start_time'             => I('post.start_time'),
                'end_time'              => I('post.end_time'),
                'certification'         => I('post.certification'),
            );

            if (!pre_data($data, ['end_time'])) {
                $this->error("请完善必要信息");
            }

            if ($id) {
                // 更新
                M('pf_certificate')->where(['id' => $id, 'del_time' => 0])->save($data);
                if (sp_is_mobile()) {
                    $this->success('编辑成功', U('Professor/Professor/professor_edit'));exit;
                }
                $data = array('status' => 1, 'id' => $id);
                $this->ajaxReturn($data);
            } else {
                // 新建
                $id = M('pf_certificate')->add($data);
                if (sp_is_mobile()) {
                    $this->success('编辑成功', U('Professor/Professor/professor_edit'));exit;
                }
                $data = array('status' => 1, 'id' => $id);
                $this->ajaxReturn($data);
            }

        } else {
            if ($id = I("get.id", 0, 'intval')) {
                $pf_certificate = M('pf_certificate')->where(['id' => $id, 'del_time' => 0])->find();
                $this->assign('pf_certificate', $pf_certificate);
            }
            $this->display();
        }
    }

    // 编辑专栏作者专利发明
    public function pf_patent_edit()
    {
        if (IS_POST) {
            $id = $_POST['id'];

            // 删除
            if ($_POST['delete']) {
                if ($info = M('pf_patent')->where(['id' => $id, 'del_time' => 0])->find()) {
                    $data['del_time'] = time();
                    M('pf_patent')->where(['id' => $id])->save($data);
                    $this->success("删除成功");
                } else {
                    $this->error("请求数据有误！");
                }
            }

            $data = array(
                'user_id'           => sp_get_current_userid(),
                'pf_id'             => $this->pf_id,
                'patent_name'       => I('post.patent_name'),
                'patent_country'    => I('post.patent_country'),
                'patent_no'         => I('post.patent_no'),
                'patent_time'       => I('post.patent_time'),
            );

            if (!pre_data($data)) {
                $this->error("请完善必要信息");
            }

            if ($id) {
                // 更新
                M('pf_patent')->where(['id' => $id, 'del_time' => 0])->save($data);
                if (sp_is_mobile()) {
                    $this->success('编辑成功', U('Professor/Professor/professor_edit'));exit;
                }
                $data = array('status' => 1, 'id' => $id);
                $this->ajaxReturn($data);
            } else {
                // 新建
                $id = M('pf_patent')->add($data);
                if (sp_is_mobile()) {
                    $this->success('编辑成功', U('Professor/Professor/professor_edit'));exit;
                }
                $data = array('status' => 1, 'id' => $id);
                $this->ajaxReturn($data);
            }

        } else {
            if ($id = I("get.id", 0, 'intval')) {
                $pf_patent = M('pf_patent')->where(['id' => $id, 'del_time' => 0])->find();
                $this->assign('pf_patent', $pf_patent);
            }
            $this->display();
        }
    }

    // 编辑专栏作者荣誉奖项
    public function pf_award_edit()
    {
        if (IS_POST) {
            $id = $_POST['id'];

            // 删除
            if ($_POST['delete']) {
                if ($info = M('pf_award')->where(['id' => $id, 'del_time' => 0])->find()) {
                    $data['del_time'] = time();
                    M('pf_award')->where(['id' => $id])->save($data);
                    $this->success("删除成功");
                } else {
                    $this->error("请求数据有误！");
                }
            }

            $data = array(
                'user_id'               => sp_get_current_userid(),
                'pf_id'                 => $this->pf_id,
                'award_name'            => I('post.award_name'),
                'award_organization'    => I('post.award_organization'),
                'award_time'            => I('post.award_time'),
                'award_desc'            => I('post.award_desc'),
            );

            if (!pre_data($data, ['award_desc'])) {
                $this->error("请完善必要信息");
            }

            if ($id) {
                // 更新
                M('pf_award')->where(['id' => $id, 'del_time' => 0])->save($data);
                if (sp_is_mobile()) {
                    $this->success('编辑成功', U('Professor/Professor/professor_edit'));exit;
                }
                $data = array('status' => 1, 'id' => $id);
                $this->ajaxReturn($data);
            } else {
                // 新建
                $id = M('pf_award')->add($data);
                if (sp_is_mobile()) {
                    $this->success('编辑成功', U('Professor/Professor/professor_edit'));exit;
                }
                $data = array('status' => 1, 'id' => $id);
                $this->ajaxReturn($data);
            }

        } else {
            if ($id = I("get.id", 0, 'intval')) {
                $pf_award = M('pf_award')->where(['id' => $id, 'del_time' => 0])->find();
                $this->assign('pf_award', $pf_award);
            }
            $this->display();
        }
    }

    // 专栏作者个人主页
    public function professor_homepage()
    {
        $user_id = get_current_userid();
        if ($pf_info = M('professor')->where(['user_id' => $user_id])->find()) {
            $this->assign('pf_info', $pf_info);
            $this->display();
        } else {
            $this->error('请先完成专栏作者认证');
        }
    }

    // 将新增加的专栏作者的相关信息表中的pf_id赋值
    public function _link_pf_info($pf_id)
    {
        $data   = ['pf_id' => $pf_id];
        $where  = ['user_id' => sp_get_current_userid()];

        // 项目
        M('pf_project')->where($where)->save($data);

        // 教育
        M('pf_education')->where($where)->save($data);

        // 证书
        M('pf_certificate')->where($where)->save($data);

        // 出版刊物
        M('pf_publish')->where($where)->save($data);

        // 专栏发明
        M('pf_patent')->where($where)->save($data);

        // 荣誉奖项
        M('pf_award')->where($where)->save($data);
    }
}